/**
 * Created by meiguizhinian on 2018/6/28
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

ivy.BaseAniPlayer = cc.Node.extend({
	ctor : function () {
		this._super();
		this._suitChangeMap = new LLDictionary();
		this._playerAniChildrenVec = new LLArray();
		this.blendFunc_ = new cc.BlendFunc(0, 0);
	},

	getPlayerType : function() {
		return ivy.AniPlayerType.eNone;
	},

	clone : function(isHold=false) {
		return null;
	},

	reset : function() {},
	resetAnimation : function(id){},
	setPlayState : function(state, autoResetSeqID=true) {},

	getPlayState : function() {
		return this._playState;
	},

	setPlayType : function(type, autoResetSeqID=true) {},

	getPlayType : function() {
		return this._playType;
	},

	autoSetSequenceID : function() {},

	setActionID : function( actionID, resetSequence=true) {},
	getActionID : function() {
		return this.actionID_;
	},

	getActionIDBak : function() {
		return this._actionIDBak;
	},

	getActionCount : function() {
		return 0;
	},

	getSequenceCount : function(actionID) {
		return 0;
	},

	setAnimationID : function(aniID) {
		this._aniID = aniID;
	},

	getAnimationID : function() {
		return this._aniID;
	},

	setSequenceID : function(seqID) {},

	getSequenceID : function() {
		return this._sequenceID;
	},

	setNeedRefreshSpriteChildren : function(need) {
		this._needRefreshSpriteChildren = need;
	},

	getNeedRefreshSpriteChildren : function() {
		return this._needRefreshSpriteChildren;
	},

	getSuitChangeID : function(imgID) {
		return 0;
	},

	setSuitChange : function(imgID, suitID) {},

	clearSuitChangeData : function() {},

	isFlippedX : function() {
		return this.isFilppedX;
	},

	setFlippedX : function(filp) {
		if (filp !== this.isFilppedX) {
			this.isFilppedX = filp;
			this._needRefreshSpriteChildren = true;
		}
	},

	setSelfUpdate : function(up) {
		this._selfUpdate = up;
		if (up) {
			this.scheduleUpdate();
		} else {
			this.unscheduleUpdate();
		}
	},

	getSelfUpdate : function() {
		return this._selfUpdate;
	},

	setAnimationData : function(aniData) {
		this._animationData = aniData;
	},

	getAnimationData : function() {
		return this._animationData;
	},

	getPlayerChildrenVec : function() {
		return this._playerAniChildrenVec;
	},

	isPlayOver : function() {
			return this._isPlayEnd;
	},

	getSequenceDurationValue : function() {
		return this._actionSequenceDuration;
	},

	isKeyFrameDataNeedToDeal()  {
		return false;
	},

	/**
	 * getKeyFrameBaseData(type)
	 * getKeyFrameBaseData(actionID, seqID, type)
	 * */
	getKeyFrameBaseData : function(typeOrActionID, seqID, type) {
		return 0;
	},
	/**
	 * getKeyFrameCustomData(type)
	 * getKeyFrameCustomData(actionID, seqID, type)
	 * */
	getKeyFrameCustomData : function(typeOrActionID, seqID, type) {
		return 0;
	},
	/**
	 * getKeyFrameCustomZoneData(type)
	 * getKeyFrameCustomZoneData(actionID, seqID, type)
	 * */
	getKeyFrameCustomZoneData : function(typeOrActionID, seqID, type) {
		return cc.rect();
	},
	/**
	 * isKeyFrameBaseDataExist(type)
	 * isKeyFrameBaseDataExist(actionID, seqID, type)
	 * */
	isKeyFrameBaseDataExist : function(typeOrActionID, seqID, type) {
		return false;
	},
	/**
	 * isKeyFrameCustomDataExist(type)
	 * isKeyFrameCustomDataExist(actionID, seqID, type)
	 * */
	isKeyFrameCustomDataExist : function(typeOrActionID, seqID, type) {
		return false;
	},
	/**
	 * isKeyFrameCustomZoneDataExist(type)
	 * isKeyFrameCustomZoneDataExist(actionID, seqID, type)
	 * */
	isKeyFrameCustomZoneDataExist : function(typeOrActionID, seqID, type) {
		return false;
	},

	getAttackBox : function() {
		return cc.rect();
	},
	getCollisionBox : function() {
		return cc.rect();
	},

	getMotionValue : function() { return this._motionValue; },
	setMotionValue : function(mV=1.0) {
		this._motionValue = mV;
	},
	setExDuration : function(d) {
		this._exDuration = d;
	},
	getExDuration : function() {
		return this._exDuration;
	},

	setShowColAtkBoxRect : function(show){},

	setPlayOverFunction : function(func) {
		this._playOverFunc = func;
	},
	//对当前动画播放器设置混合绘制 所有子节点将使用当前设置进行混合
	setCustomBlendFunc : function(blendFunc) {
		this._useCustomBlendFunc = true;
		this.blendFunc_ = blendFunc;
	},
	getCustomBlendFunc : function() {
		return this.blendFunc_;
	},

	getSuitChangeMap : function() {
		return this._suitChangeMap;
	},
	setSuitChangeMap : function(map) {
		this._suitChangeMap.assign(map);
	},
	setCustomUpdateAniPlayerChildrenSpriteFunc : function(func) {
		this._customUpdateAniChildrenSpriteFunc = func;
	},
	getLifeTimer : function() {
		return this.lifeTimer_;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	_selfUpdate : true,
	isFilppedX : false,

	_aniID : 0,
	actionID_ : 0,
	_actionIDBak : 0,
	_sequenceID : 0,
	_actionSequenceDuration : 0,
	_motionValue : 1.0,
	_exDuration : 0,
	_isPlayEnd : false,
	_needRefreshSpriteChildren : true,
	_playState : ivy.AniPlayState.eCircle,
	_playType : ivy.AniPlayType.eNormal,
	_animationData : null,
	_suitChangeMap : null,
	_playerAniChildrenVec : null,
	//////////////////////////////////////////////////////////////////////////
	showColAtkBox : false,
	colBoxDrawNode : null,
	atkBoxDrawNode : null,
	//////////////////////////////////////////////////////////////////////////
	_playOverFunc : null,
	//////////////////////////////////////////////////////////////////////////
	_useCustomBlendFunc : false,
	blendFunc_ : null,
	//////////////////////////////////////////////////////////////////////////
	_customUpdateAniChildrenSpriteFunc : null,
	lifeTimer_ : 0,
});

ivy.BaseAniPlayer.create = function (ani, actionID=0, sequenceID=0, selfUpdate=true, playState=ivy.AniPlayState.eCircle, playType=ivy.AniPlayType.eNormal) {
	if (ani instanceof ivy.AnimationID) {
		return this.create(ani.classID, ani.actionID, ani.sequenceID, ani.selfUpdate, ani.playState, ani.playType);
	} else {
		if (cc.isNumber(ani)) {
			let classID = ani;
			let aniFileName = ivy.SceneCommonDataManager.getInstance().getAniFileNameByClassID(classID);

			let aniFilePath = ivy.GlobleConfig.getInstance().getAnimationDataPath(aniFileName);
			if (cc.FileUtils.getInstance().isFileExist(aniFilePath)){
				return ivy.ClipAniPlayer.create(classID, actionID, sequenceID, selfUpdate, playState, playType);
			} else {
				let spineAniFilePath = ivy.GlobleConfig.getInstance().getSpineAnimationDataPath(aniFileName);
				if (cc.FileUtils.getInstance().isFileExist(spineAniFilePath)) {
					return ivy.SpineAniPlayer.create(classID, actionID, sequenceID, selfUpdate, playState, playType);
				}
			}
		} else if (cc.isString(ani)) {
			let aniFileName = ani;

			let aniFilePath = ivy.GlobleConfig.getInstance().getAnimationDataPath(aniFileName);
			if (cc.FileUtils.getInstance().isFileExist(aniFilePath)){
				return ivy.ClipAniPlayer.create(aniFileName, actionID, sequenceID, selfUpdate, playState, playType);
			} else{
				let spineAniFilePath = ivy.GlobleConfig.getInstance().getSpineAnimationDataPath(aniFileName);
				if (cc.FileUtils.getInstance().isFileExist(spineAniFilePath)) {
					return ivy.SpineAniPlayer.create(aniFileName, actionID, sequenceID, selfUpdate, playState, playType);
				}
			}
		}
	}
	return null;
};

ivy.BaseAniPlayer.getPlayerType = function(ids) {
	let aniFileName = ivy.SceneCommonDataManager.getInstance().getAniFileNameByClassID(ids.classID);
	let aniFilePath = ivy.GlobleConfig.getInstance().getAnimationDataPath(aniFileName);
	if (cc.FileUtils.getInstance().isFileExist(aniFilePath)) {
		return ivy.AniPlayerType.eClip;
	} else{
		let spineAniFilePath = ivy.GlobleConfig.getInstance().getSpineAnimationDataPath(aniFileName);
		if (cc.FileUtils.getInstance().isFileExist(spineAniFilePath)) {
			return ivy.AniPlayerType.eSpine;
		}
	}
	return ivy.AniPlayerType.eNone;
};